// orders.js
var jsonFormat = require('../../utils/jsonFormat.js');
var pay = require('../../utils/pay.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: getApp().globalData.host,
    platUser: {},
    page: {
      pageNo: 1,
      pageSize: 8
    },
    queryStatus: '',
    orders: [],
    hidden: false,
    scrollTop: 0,
    scrollHeight: 0,
    hasMore: true,
    types:[1,2,3,8,9,11,15,21]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.setData({
      platUser: getApp().globalData.platUser,
      types: typeof (options.types) == "undefined" ? '' : JSON.parse(options.types)
    })
    this.getData();
  },

  getData: function () {
    var that = this;
    wx.request({
      url: that.data.host + 'webapp/orders/list.html',
      data: { buyer: that.data.platUser.userId, status: that.data.queryStatus, pageSize: that.data.page.pageSize, 
        pageNo: that.data.page.pageNo ,types:that.data.types},
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        var orders = that.data.orders;
        if (res.data.results == '' || res.data.results == null || typeof(res.data.results)=='undefined' ||res.data.results.length==0) {
          that.setData({
            hidden: true,
            hasMore: false
          });
          //if(orders.length>0)
          return;
        }
        jsonFormat.format(res.data,res.data);
        for (var i = 0; i < res.data.results.length; i++) {
          if(typeof(res.data.results[i].orderSkus)!='undefined'&&res.data.results[i].orderSkus.length>0){
            for(let j = 0;j<res.data.results[i].orderSkus.length;j++){
              res.data.results[i].orderSkus[j].imgUrl = util.imageUrl(res.data.results[i].orderSkus[j].imgUrl);
            }
          }
          orders.push(res.data.results[i]);
        }
        that.data.page.pageNo++;
        that.setData({
          orders: orders,
          hidden: true,
          hasMore:true
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  //查看订单详情
  getOrderDetail: function (event) {
    var index = event.currentTarget.dataset.index;
    if('NormalCustomizationWarehouseReceiptForBiddingWarehouseReceiptForListing'.indexOf(this.data.orders[index].type)>-1)
      wx.navigateTo({
        url: '../order/order?orderId=' + this.data.orders[index].orderId,
      })
  },
  //查看物流详情
  showExpress:function(e){
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../express/express?orderId=' + this.data.orders[index].orderId,
    })
  },
  //重新查询订单列表
  queryList: function (event) {
    var clickTab = event.currentTarget.dataset.querystatus;
    if (clickTab == this.data.queryStatus)
      return;
    //初始化当前页
    this.setData({
      queryStatus: clickTab,
      hidden: false,
      orders: [],
      page: {
        pageNo: 1,
        pageSize: 8
      },
      hasMore: true
    });
    this.getData();
  },
  //页面滑动到底部
  bindDownLoad: function () {
    var that = this;

    if (that.data.hasMore) {
      that.getData();
      //查询期间设置为不得再次查询
      that.setData({
        hasMore: false
      });

    }
  },
  //去付款
  doPay:function(e){
    var index = e.currentTarget.dataset.index;
    //console.log("index is : " + index + "| order is : " + this.data.orders[index].orderCode);
    //待付款订单无需收货地址
    pay.toPay(this.data.orders[index],'');
  },
  //确认收货
  confirmDelivery:function(e){
    var index = e.currentTarget.dataset.index;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认已收到商品？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: that.data.host + 'bizportal/my/buyer/order/confirmDelivery',
            data: { orderId: that.data.orders[index].orderId},
            header: getApp().globalData.header,
            method: 'POST',
            success: function (res) {
              if(res.data.errorCode==0){
                that.data.orders[index].status = 'Received';
                that.setData({
                  orders: that.data.orders
                });
              }else{
                wx.showToast({
                  title: "确认失败",
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { }
          })
        }
      }
    })
  }
})